package com.bocloud.cmp.driver.providers.vmware;

import java.util.List;

import com.bocloud.cmp.driver.providers.AlarmDriver;
import com.bocloud.cmp.model.AlarmModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;
import com.vmware.vim25.AlarmInfo;
import com.vmware.vim25.AlarmState;
import com.vmware.vim25.mo.Alarm;
import com.vmware.vim25.mo.ClusterComputeResource;
import com.vmware.vim25.mo.Datacenter;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.HostSystem;
import com.vmware.vim25.mo.Network;
import com.vmware.vim25.mo.ResourcePool;
import com.vmware.vim25.mo.VirtualMachine;

/**
 * vmware告警操作接口实现类
 * 
 * @author weiwei
 *
 */
public class VmwareAlarmDriver extends VmwareDriver implements AlarmDriver {

	public VmwareAlarmDriver() {
		super();
	}

	/**
	 * @param endpoint
	 * @param username
	 * @param password
	 */
	public VmwareAlarmDriver(String endpoint, String username, String password) {
		super(endpoint, username, password);
	}

	@Override
	public BsmResult sync() {
		// 存放告警信息
		List<AlarmModel> alarmList = Lists.newArrayList();
		Folder rootFolder = this.getRootFolder();
		AlarmState[] alarmStates = rootFolder.getTriggeredAlarmState();
		if (null != alarmStates) {
			for (AlarmState alarmState : alarmStates) {
				// alarmModel类
				AlarmModel alarmModel = new AlarmModel();
				// 创建alarm对象
				Alarm alarm = new Alarm(rootFolder.getServerConnection(), alarmState.getAlarm());
				// 获取告警信息
				AlarmInfo alarmInfo = alarm.getAlarmInfo();
				alarmModel.setName(alarmInfo.getName());// 告警名称
				alarmModel.setRemark(alarmInfo.getDescription());// 告警描述
				alarmModel.setLevel(alarmState.getOverallStatus().toString());// 告警等级
				alarmModel.setAlarmVal(alarmInfo.getKey());// 告警key值
				alarmModel.setGmtTrigger(alarmState.getTime().getTime());// 告警触发时间
				// 告警确认时间
				if (null != alarmState.getAcknowledgedTime()) {
					alarmModel.setGmtConfirm(alarmState.getAcknowledgedTime().getTime());
				}
				// 告警确认人
				if (null != alarmState.getAcknowledgedByUser()) {
					alarmModel.setConfirmer(alarmState.getAcknowledgedByUser());
				}
				// 判断不同类型的告警
				if (alarmState.getEntity().getType().equals("HostSystem")) {
					HostSystem host = new HostSystem(rootFolder.getServerConnection(), alarmState.getEntity());
					alarmModel.setTarget(host.getName());
					alarmModel.setType("HostSystem");
				} else if (alarmState.getEntity().getType().equals("VirtualMachine")) {
					VirtualMachine vm = new VirtualMachine(rootFolder.getServerConnection(), alarmState.getEntity());
					alarmModel.setTarget(vm.getName());
					alarmModel.setType("VirtualMachine");
				} else if (alarmState.getEntity().getType().equals("Datastore")) {
					Datastore datastore = new Datastore(rootFolder.getServerConnection(), alarmState.getEntity());
					alarmModel.setTarget(datastore.getName());
					alarmModel.setType("Datastore");
				} else if (alarmState.getEntity().getType().equals("Folder")) {
					Folder folder = new Folder(rootFolder.getServerConnection(), alarmState.getEntity());
					alarmModel.setTarget(folder.getName());
					alarmModel.setType("Folder");
				} else if (alarmState.getEntity().getType().equals("Datacenter")) {
					Datacenter datacenter = new Datacenter(rootFolder.getServerConnection(), alarmState.getEntity());
					alarmModel.setTarget(datacenter.getName());
					alarmModel.setType("Datacenter");
				} else if (alarmState.getEntity().getType().equals("Network")) {
					Network network = new Network(rootFolder.getServerConnection(), alarmState.getEntity());
					alarmModel.setTarget(network.getName());
					alarmModel.setType("Network");
				} else if (alarmState.getEntity().getType().equals("ResourcePool")) {
					ResourcePool resourcePool = new ResourcePool(rootFolder.getServerConnection(),
							alarmState.getEntity());
					alarmModel.setTarget(resourcePool.getName());
					alarmModel.setType("ResourcePool");
				} else if (alarmState.getEntity().getType().equals("ClusterComputeResource")) {
					ClusterComputeResource cluster = new ClusterComputeResource(rootFolder.getServerConnection(),
							alarmState.getEntity());
					alarmModel.setTarget(cluster.getName());
					alarmModel.setType("ClusterComputeResource");
				}
				alarmList.add(alarmModel);
			}
		}
		this.getServiceInstance().getServerConnection().logout();
		return new BsmResult(true, alarmList, "获取告警信息成功");
	}

}
